import { createApp } from 'vue';
import App from './App.vue';
import Antd from 'ant-design-vue';
import {APP_CONFIG,HEADER_THEME } from '@/constants/system-const';
import { router } from '@/router';
import { store } from '@/store';
import lodash from 'lodash';
import 'ant-design-vue/dist/reset.css';
import '@/assets/styles/theme.less';
import * as antIcons from '@ant-design/icons-vue';

import { loginApi } from '@/api/login-api';
import { useUserStore } from '@/store/modules/user';
import tool from './utils/tool';

function initVue() {
	const app = createApp(App);
	// 注册图标组件
	Object.keys(antIcons).forEach((key) => {
	app.component(key, antIcons[key]);
	});
	app.config.globalProperties.$antIcons = antIcons;
	app.config.globalProperties.$lodash = lodash;
	app.use(Antd);
	app.use(router);
	app.use(store);
	app.mount('#app');

	HEADER_THEME.forEach(item=>{
		if(item.name == APP_CONFIG.headTheme){
			APP_CONFIG.headBackground=item.headBackground;
			APP_CONFIG.headColor=item.headColor;
			return false;
		}
	});
 
}

async function getLoginInfo(userInfo) {
	try {
		userInfo = JSON.parse(userInfo);
		initVue();
		//更新用户信息到pinia
		useUserStore().setUserLoginInfo(userInfo);
	} catch (e) {
		tool.error(e.data ? e.data.msg : e.message);
		tool.captureError(e);
		initVue();
	}
}
const userInfo = tool.readLocalStorage("USER_INFO"); 
if (userInfo) {
    getLoginInfo(userInfo);
} else {
	initVue();
}
